package com.onpositive.wiki3.db.propaccess;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.carrotsearch.hppc.IntIntOpenHashMap;
import com.carrotsearch.hppc.IntObjectOpenHashMap;
import com.onpositive.wiki3.db.IWikiDB;
import com.onpositive.wiki3.db.impl.WikiDBImpl;

public class PropsProviderImpl implements IPropsProvider {

	protected IntIntOpenHashMap objToStart = new IntIntOpenHashMap();
	HashMap<String, Integer> propStart = new HashMap<String, Integer>();
	protected RandomAccessFile file;
	protected RandomAccessFile file1;

	@SuppressWarnings("unchecked")
	public PropsProviderImpl(String path) {
		try {
			ObjectInputStream is = new ObjectInputStream(
					new BufferedInputStream(new FileInputStream(path
							+ "/byprop.cat")));
			propStart = (HashMap<String, Integer>) is.readObject();

			is.close();
			file1 = new RandomAccessFile(new File(path + "/byprop.dat"), "r");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		PropsProviderImpl propsProviderImpl = new PropsProviderImpl(
				"C:/work/content/props/");
		IntObjectOpenHashMap<Object> values = propsProviderImpl
				.getValues("max speed");
		IWikiDB db = new WikiDBImpl("C:/work/content/");
		for (int q : values.keys().toArray()) {
			Object object = values.get(q);
			try{
				String replace = object.toString().trim().replace(',', '.');
				
				for (int a=0;a<replace.length();a++){
					
				}
				Double.parseDouble(replace);
			}catch (Exception e) {
				// TODO: handle exception
			}
//			System.out.println(db.getTitleMapper().getTitle(q) + ":"
//					+ object);
		}

		System.out.println(values.size());
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Map<String, Object> getProperties(int object) {
		int i = objToStart.get(object);
		if (i > 0) {
			try {
				file.seek(i);
				int sz = file.readInt();
				byte[] dta = new byte[sz];

				file.readFully(dta);
				DataInputStream ss = new DataInputStream(
						new ByteArrayInputStream(dta));
				int readInt = ss.readInt();
				HashMap<String, String> sm = new HashMap<String, String>();
				for (int a = 0; a < readInt; a++) {
					sm.put(ss.readUTF(), ss.readUTF());
				}
				return (Map) sm;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// TODO Auto-generated method stub
		return null;
	}
	
	public Set<String>getPropNames(){
		return propStart.keySet();
	}

	@SuppressWarnings("unchecked")
	@Override
	public IntObjectOpenHashMap<Object> getValues(String property) {
		Integer integer = propStart.get(property);
		if (integer==null){
			return new IntObjectOpenHashMap<Object>();
		}
		int i = integer;
		try {
			file1.seek(i);
			int sz = file1.readInt();
			byte[] dta = new byte[sz];

			file1.readFully(dta);
			DataInputStream ss = new DataInputStream(new ByteArrayInputStream(
					dta));
			IntObjectOpenHashMap<String> str = new IntObjectOpenHashMap<String>();
			try {
				while (true) {
					int q = ss.readInt();
					String readUTF = ss.readUTF();
					str.put(q, readUTF);
				}
			} catch (EOFException e) {
				return (IntObjectOpenHashMap) str;
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
		return null;
	}

}
